package com.gitee.huanminabc.utils_tools.redis.config;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Configuration
@ComponentScan("com.gitee.huanminabc.utils_tools.redis")
@ConfigurationProperties(prefix = "spring.redis")
@Data
@Slf4j
public class RedisAutoConfiguration {
    private Integer dbIndex=0;
    //用于分布式锁的唯一标识,一般使用token如果没有找到,就找sessionId
    private String tokenName;
    //使用的模式: single,sentinel,cluster
    private String model="single";

    //哨兵主节点名称
    private String sentinelMaster;

    //单机使用的节点
    private String singleNode;

    private boolean switchDb=false;


    private PoolRedisConfig pool = new PoolRedisConfig();

    private List<Map<String, NodeRedisConfig>> nodes=new ArrayList<>();


    @Data
    public static class PoolRedisConfig {
        private int maxActive;
        private int maxIdle;
        private int minIdle;
        private int maxWait;
        private int timeout;
        private int shutdownTimeout;
    }
    @Data
    public static class NodeRedisConfig {
        private String host;
        private int port;
        private String password;
    }

    //通过node名称获取节点信息
    public NodeRedisConfig getNodeConfig(String nodeName){
        for (Map<String, NodeRedisConfig> node : nodes) {
            if(node.containsKey(nodeName)){
                return node.get(nodeName);
            }
        }
        return null;
    }



}
